package com.yh.supergo.portal.controller;

import com.yh.supergo.http.HttpResult;
import com.yh.supergo.manager.feign.ItemCatFeignClient;
import com.yh.supergo.pojo.Itemcat;
import com.yh.supergo.user.utils.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author 0715-YuHao
 * @version 1.0
 * @className ItemCatController
 * @description TODO
 * @date 2020/11/16 22:42
 */
@RestController
public class ItemCatController {

    @Autowired
    private ItemCatFeignClient itemCatFeignClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("category/category")
    public HttpResult getItemCatList() {
        //查询商品分类之前可以先查询缓存
        String json = redisTemplate.opsForValue().get("portal-itemcats");
        //如果缓存中有数据，直接返回
        if (StringUtils.isNotBlank(json)) {
            List<Map> list = JsonUtils.jsonToList(json, Map.class);
            return HttpResult.ok(list);
        }
        //如果缓存中没有数据，查询数据
        List<Itemcat> list = itemCatFeignClient.getItemCatList();
        //把数据放到缓存中
        redisTemplate.opsForValue().set("portal-itemcats", JsonUtils.objectToJson(list));
        //返回结果
        return HttpResult.ok(list);
        /*List<Itemcat> itemCatList = itemCatFeignClient.getItemCatList();
        return HttpResult.ok(itemCatList);*/
    }
}
